<template>
  <a-spin :spinning="confirmLoading">
    <div class="top-row">
      <a-form-item :colon="false" label="设备名称">
        {{model.shebeiname}}
      </a-form-item>
      <a-form-item :colon="false" label="设备编号">
        {{ model.shebeibianhao }}
      </a-form-item>
      <a-form-item :colon="false" label="计划名称">
        {{ model.name }}
      </a-form-item>
      <a-form-item :colon="false" label="计划编号">
        {{ model.bianhao }}
      </a-form-item>
      <a-form-item :colon="false" label="保养层级">
        {{ model.cengji_dictText }}
      </a-form-item>
      <a-form-item :colon="false" label="保养方式">
        {{ model.fangshi_dictText }}
      </a-form-item>
      <a-form-item :colon="false" label="间隔天数">
        {{ model.jiangeshichang }}
      </a-form-item>
    </div>
    <j-form-container :disabled="true">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" style="padding-top: 15px;">
        <a-row>
          <a-col :span="12">
            <a-form-model-item  label="执行人"  style="display: block; margin-bottom: 10px;" prop="fuzeren">
              <a-input v-model="model.fuzeren_dictText" placeholder="确定后自动生成"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item  label="保养时间"  style="display: block; margin-bottom: 10px;" prop="zhixingshijian">
              <j-date
                placeholder="确定后自动生成"
                v-model="model.zhixingshijian"
                :show-time="true"
                date-format="YYYY-MM-DD "
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" style="padding-top: 15px;">
        <a-row>
          <a-col :span="12">
            <a-form-model-item  label="保养说明"  style="display: block; margin-bottom: 10px;" prop="beizhu">
              <a-textarea v-model="model.beizhu" placeholder="请输入保养说明" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <div style="display: flex;justify-content: space-between;align-items: center;">
        </div>
<!--        <div style="display: flex;justify-content: space-between;align-items: center;">-->
<!--        <div style="border-left: 3px solid RGB(0,92,230);padding-left:8px;">更换备件</div>-->
<!--        <j-popup-->
<!--          v-model="model.a"-->
<!--          v-if="!disabled"-->
<!--          field="a"-->
<!--          org-fields="name,id,model,wsnub"-->
<!--          dest-fields="name,deviceId,model,wsnub"-->
<!--          code="ene_spare_parts"-->
<!--          :multi="false"-->
<!--          placeholder="选择备品备件"-->
<!--          @input="popupCallback"-->
<!--          style="width:150px;"-->
<!--        />-->
<!--      </div>-->
        <div v-if="dataSource && dataSource.length > 0" style="margin-top:5px;">
          <a-table rowKey="key" bordered :data-source="dataSource" :columns="columns" :pagination="false" size="small">
            <template slot="usenum" slot-scope="text, record">
              <div class="editable-cell">
                <div v-if="record.editable" class="editable-cell-input-wrapper">
                  <a-input-number
                    v-model="record.usenum"
                    @change="handleChange"
                    @pressEnter="onCellChange(record, 'usenum')"
                  /><a-icon type="check" class="editable-cell-icon-check" @click="onCellChange(record, 'usenum')" />
                </div>
                <div v-else class="editable-cell-text-wrapper">
                  {{ text }}
                  <a-icon type="edit" class="editable-cell-icon" @click="edit1(record)" />
                </div>
              </div>
            </template>
            <template slot="operation" slot-scope="text, record">
              <a-popconfirm v-if="dataSource.length" title="确定要删除吗?" @confirm="() => onDelete(record.key)">
                <a href="javascript:;" style="color: red">删除</a>
              </a-popconfirm>
            </template>
          </a-table>
        </div>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'GdBaoyangRenwuForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/gongdan/gdBaoyangRenwu/add",
        edit: "/gongdan/gdBaoyangRenwu/edit",
        queryById: "/gongdan/gdBaoyangRenwu/queryById"
      },
      columns: [
        {
          title: '名称',
          dataIndex: 'name',
          key: 'name'
        },
        {
          title: '规格',
          dataIndex: 'model',
          key: 'model'
        },
        {
          title: '库存',
          dataIndex: 'wsnub',
          key: 'wsnub'
        },
        {
          title: '数量',
          dataIndex: 'usenum',
          key: 'usenum',
          width: 150,
          scopedSlots: { customRender: 'usenum' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      dataSource: []
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    detail(record) {
      this.columns.pop()
      this.model = Object.assign({}, record)
      this.model.beizhu = ''
      this.columns[2].className = 'hide'
      this.repairInfo()
    },
    repairInfo() {
      getAction(this.url.queryById, { id: this.model.id }).then(res => {
        console.log(res.result,111111111)
        this.disabled = false
        this.model.beizhu = res.result.beizhu
        this.dataSource = res.result.partsList
        this.disabled = true
      })
    },
    edit1(record) {
      record.editable = true
    },
    handleChange(e) {
      // const value = e.target.value
      // this.value = value
    },
    onCellChange(record, dataIndex) {
      if (record.usenum <= 0) record.usenum = 0
      if (record.usenum > Number.parseFloat(record.wsnub)) {
        this.$message.error('库存不足')
        record.usenum = Number.parseFloat(record.wsnub)
      }

      record.editable = false
      const dataSource = [...this.dataSource]
      const target = dataSource.find(item => item.key === record.key)
      if (target) {
        target[dataIndex] = record[dataIndex]
        this.dataSource = dataSource
      }
      //console.log('onCellChange', this.dataSource)
    },
    onDelete(record) {
      this.dataSource = this.dataSource.filter(e => {
        return e.key != record
      })
    },
    add() {
      this.edit(this.modelDefault);
    },
    deal(record) {
      this.model = Object.assign({}, record)
      this.disabled = false
      //this.columns[2].className = 'show'
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    popupCallback(...e) {
      e.filter(t => t).forEach(line => {
        if (Number.parseFloat(line.wsnub) <= 0) {
          this.$message.error(line.model + '库存不足')
          return
        }
        if (this.dataSource.filter(ds => ds.key === line.deviceId).length > 0) {
          this.$message.warn('已添加' + line.model + '类型的配件')
          return
        }
        this.dataSource.push({
          key: line.deviceId,
          name: line.name,
          model: line.model,
          wsnub: line.wsnub,
          usenum: 1,
          editable: false
        })
      })
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          this.model.partsList = this.dataSource.map(ds => {
            return {
              key: ds.key,
              usenum: ds.usenum
            }
          })
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    },
  }
}
</script>
<style scoped>
.top-row {
  background: #F4F5F7;
  padding: 12px 24px;
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.top-row /deep/ .ant-form-item {
  margin-bottom: 0;
  color: #6B778C
}

.top-row /deep/ .ant-form-item-label {
  line-height: 1;
}

.top-row /deep/ .ant-form-item-control {
  line-height: 1;
  margin-top: 10px;
}

.top-row /deep/ .ant-form-item-label>label {
  color: #6B778C
}

.basic-info-box,
.system-info-box {
  padding: 20px 0;
}

.basic-info-box /deep/ .ant-form-item,
.system-info-box /deep/ .ant-form-item {
  margin-bottom: 20px;
}

.basic-info-box /deep/ .ant-form-item-label>label {
  color: #172B4D;
}

.ene-w-h-d /deep/ .ant-collapse-borderless>.ant-collapse-item {
  border: none;
}

.ene-w-h-d /deep/ .ant-collapse-header {
  background: #F4F5F7;
}

.ene-w-h-d /deep/ .ant-collapse {
  background: transparent;
}

.ene-w-h-d /deep/ .ant-input[disabled] {
  background: #ebecf0
}

.ene-w-h-d /deep/ .ant-collapse-content>.ant-collapse-content-box {
  padding: 0
}
</style>